package script.reciteWord
{
	import laya.components.Script;
	import laya.events.Event;
	import laya.maths.MathUtil;
	import laya.utils.Browser;
	
	import model.HttpErrorCode;
	import model.HttpRequestUtil;
	import model.reciteWord.ReciteDateVo;
	import model.reciteWord.ReciteWordVo;
	
	import script.ViewManager;
	
	import ui.learn.DictationPanelUI;
	
	public class DictationWordsController extends Script
	{
		private var uiSkin:DictationPanelUI;
		private var param:Object;
		private var pageSize:int = 30;

		private var curReciteWords:Array;
		private var reciteNum:int = 0;
		private var curReciteIndex:int = 0;
		public function DictationWordsController()
		{
			super();
		}
		
		override public function onStart():void
		{
			uiSkin = this.owner as DictationPanelUI;
			
			
			uiSkin.curLevel.text = param.levelName;
			
			uiSkin.examPanel.visible = false;
			uiSkin.startYear.labels = "2025,2024";
			uiSkin.endYear.labels = "2025,2024";
			
			uiSkin.startMonth.labels = "01,02,03,04,05,06,07,08,09,10,11,12";
			uiSkin.endMonth.labels = "01,02,03,04,05,06,07,08,09,10,11,12";
			
			var curdate:Date = new Date();
			var curMont:String = (curdate.getMonth()+1) >= 10?(curdate.getMonth()+1).toString():("0" + (curdate.getMonth()+1));
			
			uiSkin.startMonth.selectedLabel = curMont;
			
			uiSkin.startYear.selectedLabel = curdate.getFullYear() + "";
			if(curdate.getMonth()+1 >= 12)
			{
				uiSkin.endYear.selectedLabel = (curdate.getFullYear() + 1).toString();
				uiSkin.endMonth.selectedLabel =  "01";
				
			}
			else
			{
				uiSkin.endYear.selectedLabel = curdate.getFullYear() + "" ;
				uiSkin.endMonth.selectedLabel =  (curdate.getMonth()+2) >= 10?(curdate.getMonth()+2).toString():("0" + (curdate.getMonth()+2));
			}
			
			uiSkin.showAnswer.on(Event.CLICK,this,showAnswer);
			uiSkin.nextWordBtn.on(Event.CLICK,this,showNextWord);
			uiSkin.exitDictation.on(Event.CLICK,this,quitExam);

			uiSkin.answerlbl.on(Event.CLICK,this,function(){
				
				Browser.window.textToAudio(this.uiSkin.answerlbl.text,"en",0);

			});
			uiSkin.startBtn.on(Event.CLICK,this,onBeginTest);
		}
		
		private function onBeginTest():void
		{
			if(parseInt(uiSkin.dicNumInput.text) <= 0)
			{
				ViewManager.showAlert("请输入背诵单词数");
				return;
			}
			
			var postdata:String = "belongLevel=" + param.id + "&page=1&limit=" + pageSize + "&startTime=" + uiSkin.startYear.selectedLabel + "-" + uiSkin.startMonth.selectedLabel + "-01 00:00:00" + "&endTime=" + uiSkin.endYear.selectedLabel + "-" + uiSkin.endMonth.selectedLabel + "-01 00:00:00";
			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.getReciteDate,this,onGetDatesBack,postdata,"post");
			
		}
		
		private function onGetDatesBack(data:Object):void
		{
			if(this.destroyed)
				return;
			
			
			var result:Object = JSON.parse(data as String);
			if(result.code == HttpErrorCode.OK)
			{
				var arr:Array = [];
				
				for(var i:int=0;i < result.data.length;i++)
				{
					var reciteVo:ReciteDateVo = new ReciteDateVo(result.data[i]);
					
					arr = arr.concat(reciteVo.words);
				}
				
			}
			curReciteWords = [];
			var maxnum:int = parseInt(uiSkin.dicNumInput.text);
			curReciteIndex = 0;
			if(arr.length <= parseInt(uiSkin.dicNumInput.text))
			{
				curReciteWords = arr;
			}
			else 
			{
				while(curReciteWords.length < maxnum && arr.length > 0)
				{
					var index:int = Math.floor(Math.random() * arr.length);
					curReciteWords.push(arr[index]);
					arr.splice(index,1);
				}
			}
			if(curReciteWords.length > 0)
			{
				showExamPanel();
				updateReciteIndex();
			}
			else
			{
				ViewManager.showAlert("暂无可默写的单词，请重新选择日期");
			}
		}
		
		private function showExamPanel():void
		{
			uiSkin.examPanel.visible = true;
			uiSkin.seqNum.text = "1/" + curReciteWords.length;
			uiSkin.englishInput.text = "";
			uiSkin.answerlbl.visible = false;
			uiSkin.answerlbl.text = "";
			uiSkin.rightImg.visible = false;
			uiSkin.errorImg.visible = false;
		}
		
		private function showAnswer():void
		{
			uiSkin.answerlbl.visible = true;
		}
		private function updateReciteIndex():void
		{
			uiSkin.seqNum.text = (curReciteIndex+1) + "/" + curReciteWords.length;
			var words:ReciteWordVo = curReciteWords[curReciteIndex] as ReciteWordVo;
			uiSkin.chineseTxt.text = words.chinese;
			uiSkin.englishInput.text = "";
			uiSkin.answerlbl.visible = false;
			uiSkin.answerlbl.text = words.word;
			uiSkin.rightImg.visible = false;
			uiSkin.errorImg.visible = false;
		}
		
		private function showNextWord():void
		{
			var words:ReciteWordVo = curReciteWords[curReciteIndex] as ReciteWordVo;

			if(uiSkin.englishInput.text.replace(" ","") != words.word)
			{
				uiSkin.errorImg.visible = true;
				ViewManager.showAlert("默写错误，默写正确才能进入下一个单词");
				return;
			}
			else
			{
				uiSkin.rightImg.visible = true;
				Laya.timer.once(1000,this,function(){
					
					if(curReciteIndex < curReciteWords.length - 1)
					{
						curReciteIndex++;
						updateReciteIndex();
					}
					
				});

			}
		}
		
		private function quitExam():void
		{
			uiSkin.examPanel.visible = false;
		}
		
	}
	
}